//
//  EmotionPackage.swift
//  AnimotionKeyboard
//
//  Created by xxy on 16/6/3.
//  Copyright © 2016年 xxy. All rights reserved.
//

import UIKit

class EmotionPackage: NSObject {
/// 表情包路径
    var id:String?
    /// 表情包mingc
    var group_name_cn: String?
    /// 表情包数组
    lazy var emoticons = [Emotion]()
    //处理未定义的
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    init(dict: [String: AnyObject]) {
        super.init()
        id = dict["id"] as? String
        group_name_cn = dict["group_name_cn"] as? String
    
        if   let array  = dict["emoticons"] as? [[String: String]] {
              var index = 0
            for  var  dict in array {
                if let png  = dict["png"], let dir = id {
                   dict["png"] = dir + "/" + png
                }
                emoticons.append(Emotion(dict: dict))
                //追加删除按钮
                index += 1
                if index == 20 {
                   emoticons.append(Emotion(isRemoved: true))
                    index = 0
                }
            }
        }
        //追加空白按钮
        appendEmptyButton()
    }
    
    
    /// 追加空白按钮
    private func appendEmptyButton() {
        let  count = emoticons.count % 21
        
        //已经排满的,就不要追加
        if  emoticons.count > 0 && count == 0 {
            
            return
        }
        for _ in count..<20 {
            emoticons.append(Emotion(isEmpty: true))
        }
        //最后再添加删除按钮
        emoticons.append(Emotion(isRemoved: true))
        
    }
    
    
    /// 描述
    override var description: String{
        let keys  = ["id", "group_name_cn", "emoticons"]
        return dictionaryWithValuesForKeys(keys).description
    }

}
